package cn.kgcnews.service.impl;

import java.util.List;
import cn.kgcnews.dao.NewsDao;
import cn.kgcnews.dao.impl.NewsDaoImpl;
import cn.kgcnews.pojo.NewsDetail;
import cn.kgcnews.pojo.PageBean;
import cn.kgcnews.service.NewsService;

public class NewsServiceImpl implements NewsService {
	NewsDao newsDao = new NewsDaoImpl();

	@Override
	public List<NewsDetail> printNewsListTop14() {
		try {
			//return new ArrayList<NewsDetail>();
			return newsDao.queryNewsTop14();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public NewsDetail showNewsDetail(String id) {
		try {
			return newsDao.queryNewsByNId(id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public boolean publishNews(NewsDetail news) {
		try {
			if (newsDao.addNews(news) == 1) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public int queryTotalRecord(String searchTitle,int searchTId) {
		try {
			return newsDao.queryTotalRecord(searchTitle,searchTId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public PageBean<NewsDetail> queryNewsByPageNo(int pageNo, int pageSize,String searchTitle,int searchTId) {
		PageBean<NewsDetail> pageBean = new PageBean<NewsDetail>();
		try {
			// 设置当前页码
			pageBean.setCurrPageNo(pageNo);
			// 设置每页的记录数
			pageBean.setPageSize(pageSize);
			pageBean.setTotalCount(this.queryTotalRecord(searchTitle,searchTId));
			// 根据页码和每页记录数获取偏移量
			int offSet = (pageNo - 1) * pageSize;
			List<NewsDetail> list = newsDao.queryNewsByOffset(offSet, pageSize,searchTitle,searchTId);
			// 设置每页的数据
			pageBean.setList(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageBean;
	}

	@Override
	public int queryTotalPageCount(int pageSize,String searchTitle,int searchTId) {
		PageBean<NewsDetail> pageBean = new PageBean<NewsDetail>();
		// 总记录数
		int totalCount = this.queryTotalRecord(searchTitle,searchTId);
		// 设置每页的记录数
		pageBean.setPageSize(pageSize);
		// 计算总页数
		pageBean.setTotalCount(totalCount);
		return pageBean.getTotalPageCount();
	}
	@Override
	public boolean delDetailByNid(Integer nid) {
		int row=-1;
		try {
			row = newsDao.delDetailByNid(nid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(row==1){
			return true;
		}
		return false;
	}

}
